package server.messageContent;

import server.AccountServiceImpl;
import common.CurrencyEnum;
import common.SerializationManager;
import common.ServiceErrorException;
import common.ServiceResponseEnum;
import common.data.ResponseTypeEnum;
import common.interfaces.MessageContent;

public class WithdrawReplyMsgContent implements MessageContent {

	private byte[] data;

	public WithdrawReplyMsgContent(ServiceResponseEnum response, long accId,
			float amount, CurrencyEnum currency) throws ServiceErrorException {
		String responseString = null;
		ResponseTypeEnum responseTypeEnum = null;

		switch (response) {
			case SUCCESSFUL_DEPOSIT:
				responseString = response.getMsg() + currency.getMsg() + String.valueOf(amount) +
				" to account number " + String.valueOf(accId) + ". New balance is: " +
						AccountServiceImpl.getCurrent().BalanceEnquiry(accId) ;
				responseTypeEnum = ResponseTypeEnum.SUCCESSFUL;
				break;
				
			case NON_EXISTING_ACCOUNT:
				responseString = response.getMsg();
				responseTypeEnum = ResponseTypeEnum.ERROR;
				break;
	
			case WRONG_PASSWORD:
				responseString = response.getMsg();
				responseTypeEnum = ResponseTypeEnum.ERROR;
				break;
			case LOW_BALANCE:
				responseString = response.getMsg();
				responseTypeEnum = ResponseTypeEnum.ERROR;
				break;
	
		case SUCCESSFUL_WITHDRAW:
			responseString = response.getMsg() + currency.getMsg()+ String.valueOf(amount) + " from account number " +
			String.valueOf(accId) + ". New balance is: " + AccountServiceImpl.getCurrent().getCurrency(accId)+
			+ AccountServiceImpl.getCurrent().BalanceEnquiry(accId);
			responseTypeEnum = ResponseTypeEnum.SUCCESSFUL;
			break;

			default:
				responseTypeEnum = ResponseTypeEnum.ERROR;
				break;
		}
		byte[] responseTypeEnumStream = SerializationManager.getCurrent()
				.fromResponseTypeEnum(responseTypeEnum);
		
		byte[] responseStream = SerializationManager.getCurrent().fromString(
				responseString);
		byte[] responseLengthStream = SerializationManager.getCurrent()
				.fromInt(responseStream.length);
		int currentIndex = 0;
		
		if (responseTypeEnum == ResponseTypeEnum.ERROR){
			this.data = new byte[responseTypeEnumStream.length
			                     + responseLengthStream.length
			                     + responseStream.length];
			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex,
					responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;
					}
		else { //create response object: updated balance
			byte[] responseObjectStream = SerializationManager.getCurrent()
					.fromFloat(
							AccountServiceImpl.getCurrent().BalanceEnquiry(
									accId));
			byte[] responseObjectLengthStream = SerializationManager
					.getCurrent().fromInt(responseObjectStream.length);

			data = new byte[responseTypeEnumStream.length
					+ responseLengthStream.length 
					+ responseStream.length
					+ responseObjectLengthStream.length
					+ responseObjectStream.length];

			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex,
					responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;

			System.arraycopy(responseLengthStream, 0, data, currentIndex,
					responseLengthStream.length);
			currentIndex += responseLengthStream.length;

			System.arraycopy(responseStream, 0, data, currentIndex,
					responseStream.length);
			currentIndex += responseStream.length;

			System.arraycopy(responseObjectLengthStream, 0, data, currentIndex,
					responseObjectLengthStream.length);
			currentIndex += responseObjectLengthStream.length;

			System.arraycopy(responseObjectStream, 0, data, currentIndex,
					responseObjectStream.length);
			currentIndex += responseObjectStream.length;			
		}
	}

	@Override
	public byte[] getByte() {
		return this.data;
	}

	@Override
	public int getLength() {
		return this.data.length;
	}

}
